package com.tjrac.generalpermissions.dao;

import com.tjrac.generalpermissions.domain.PType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ZeNing
 * @create 2020-11-27 21:39
 */
@Repository
public interface PTypeDao {

    @Select("select * from ptype")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(
                    property = "permissions",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryByPTid")
            )
    })
    List<PType> queryAll();

    @Select("select * from ptype where `id` = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(
                    property = "permissions",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryByPTid")
            )
    })
    PType queryById(int id);

    @Select("select * from ptype where `name` = #{name}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "desc", property = "desc"),
            @Result(
                    property = "permissions",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryByPTid")
            )
    })
    PType queryByName(String name);

    @Insert("insert into ptype(`name`, `desc`) values(#{name}, #{desc})")
    int addPType(PType pType);

    @Delete("delete from ptype where `id` = #{id}")
    int delById(int id);

    @Update("update ptype set `name` = #{name}, `desc` = #{desc} where `id` = #{id}")
    int modifyById(PType pType);

}
